#include "threadPool.h"




void *threadFunc(void *arg){
    threadPool_t * pthreadPool = (threadPool_t *)arg;
    while(1){
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);
        //        pthread_cleanup_push(unlock,pthreadPool);
        while(pthreadPool->taskQueue.queueSize == 0 && pthreadPool->exitFlag == 0){
            pthread_cond_wait(&pthreadPool->cond, &pthreadPool->mutex);
        }
        if(pthreadPool->exitFlag == 1){
            printf("I am worker. I am going to exit!\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        netfd = pthreadPool->taskQueue.pFront->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //pthread_cleanup_pop(1);
        printf("I am worker! I got netfd = %d\n", netfd);
        //这里写函数
        int cmd=-1;
        int length=-1;//传参的长度
        int len=-1;//专门处理最后发送
        char addr[1000]={0};
        char now_addr[1024]={"."};
        char buf[4096]={0};
        int ret;
        //char filename[100];
        while(1)
        {
            cmd=-1;
            sleep(1);
            
            recvCommandPacket(netfd,&cmd,&length,addr);
            //sleep(2);
            printf("cmd=%d\n",cmd);
            //printf("length=%d\n",length);
            switch(cmd)
            {
            case 0://cd
                //printf("cd\n");
                cdFunc(addr,length,now_addr);
                //len=strlen(now_addr);
                //send(netfd,&len,sizeof(int),MSG_WAITALL);
                //send(netfd,now_addr,len,MSG_WAITALL);
                break;
            case 1://ls
                //printf("ls\n");
                lsFunc(now_addr,addr,length,buf);
                len=strlen(now_addr);
                send(netfd,&len,sizeof(int),MSG_WAITALL);
                send(netfd,now_addr,len,MSG_WAITALL);
                break;
            case 2://pwd
                printf("pwd\n");
                pwdFunc(now_addr,buf);
                len=strlen(buf);
                send(netfd,&len,sizeof(int),MSG_WAITALL);
                send(netfd,buf,len,MSG_WAITALL);
                break;
            case 3://mkdir
                   //这里的addr，等于文件名,后面的都一样
                printf("mkdir\n");
                mkdirFunc(now_addr,addr);
                break;
            case 4://rmdir
                printf("rmdir\n");
                rmdirFunc(now_addr,addr);
                break;
            case 5://puts
                printf("puts\n");
                //putsFunc(now_addr,addr);
                break;
            case 6://gets
                printf("gets\n");
                //getsFunc(now_addr,addr);
                break;
            case 7://remove
                printf("remove\n");
                //remFunc(now_addr,addr);
                break;
            case 8://exit
                printf("exit\n");
                goto end;
                //default:
                //printf("没有该命令\n");
            }
        }
end:
        close(netfd);
    }
}
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum; ++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}

